# 1950053 自动化 赵浪舟

CC := g++ -g

CPP_STD_VER := c++17

TEST_DIR := ./test
EXEC_DIR := ./exec
TEMP_DIR := $(EXEC_DIR)/temp

SERVER_DIR := ./server
COMMON_DIR := ./common

INCLUDE_DIR := ./include
# -std=c++11
JSON_INCLUDE := $(INCLUDE_DIR)/nlohmann
ASIO_INCLUDE := $(INCLUDE_DIR)/asio-1.23.0
# depend on ASIO
# -std=c++17: using std::scoped_lock
NET_INCLUDE  := $(INCLUDE_DIR)/network
# class agent map path world etc
DW_INCLUDE   := $(INCLUDE_DIR)/world


TEST_FLAGS   := -I$(TEST_DIR)
COMMON_FLAGS := -std=$(CPP_STD_VER) -I$(INCLUDE_DIR)

JSON_FLAGS := -I$(JSON_INCLUDE)
ASIO_FLAGS := -I$(ASIO_INCLUDE) -lpthread

NET_FLAGS    := -I$(ASIO_INCLUDE) -lpthread -I$(NET_INCLUDE)
DW_FLAGS     := -I$(JSON_INCLUDE) -I$(DW_INCLUDE) -lm
DW_NET_FLAGS := $(DW_FLAGS) $(NET_FLAGS)


JSON_FLAGS := $(COMMON_FLAGS) -I$(JSON_INCLUDE)
ASIO_FLAGS := $(COMMON_FLAGS) -I$(ASIO_INCLUDE) -lpthread

NET_FLAGS    := $(COMMON_FLAGS) $(NET_FLAGS)
DW_FLAGS     := $(COMMON_FLAGS) $(DW_FLAGS)
DW_NET_FLAGS := $(COMMON_FLAGS) $(DW_NET_FLAGS)

OBJS :=
EXES :=
TEST_OBJS :=
TEST_EXES :=
CLEAN_LIST = $(OBJS) $(EXES) $(TEST_OBJS) $(TEST_EXES)


################################################################################
#
#	DEFAULT COMPILE CONFIG
#
################################################################################

.PHONY 	: noarg
noarg	: all

################################################################################

NAME := json
DEPEND := 

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(JSON_FLAGS) $^ -o $@

$(TEST_EXE) : $(TEST_OBJ)
	$(CC) $(JSON_FLAGS) $^ -o $@

################################################################################

NAME := asio
DEPEND := 

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(ASIO_FLAGS) $< -o $@

$(TEST_EXE) : $(TEST_OBJ)
	$(CC) $(ASIO_FLAGS) $^ -o $@

################################################################################

NAME := message
DEPEND := $(NET_INCLUDE)/$(NAME:%=net_%.hpp)

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(NET_FLAGS) $< -o $@

$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(NET_FLAGS) $^ -o $@

################################################################################

NAME := tsdeque
DEPEND := $(NET_INCLUDE)/$(NAME:%=net_%.hpp)

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(NET_FLAGS) $< -o $@

$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(NET_FLAGS) $^ -o $@

################################################################################

NAME := server
DEPEND := $(NET_INCLUDE)/$(NAME:%=net_%.hpp)

DEPEND += $(NET_INCLUDE)/net_tsdeque.hpp
DEPEND += $(NET_INCLUDE)/net_message.hpp
DEPEND += $(NET_INCLUDE)/net_connection.hpp

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(NET_FLAGS) $< -o $@

$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(NET_FLAGS) $^ -o $@

################################################################################

NAME := client
DEPEND := $(NET_INCLUDE)/$(NAME:%=net_%.hpp)

DEPEND += $(NET_INCLUDE)/net_tsdeque.hpp
DEPEND += $(NET_INCLUDE)/net_message.hpp
DEPEND += $(NET_INCLUDE)/net_connection.hpp

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(NET_FLAGS) $< -o $@

$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(NET_FLAGS) $^ -o $@

################################################################################

NAME := agent
DEPEND := $(DW_INCLUDE)/$(NAME:%=%.hpp)
DEPEND += $(DW_INCLUDE)/agent_manager.hpp


TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(TEST_FLAGS) $(DW_FLAGS) $< -o $@

$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(TEST_FLAGS) $(DW_FLAGS) $^ -o $@

################################################################################

NAME := noise
DEPEND := $(DW_INCLUDE)/$(NAME:%=%.hpp)

SRC := $(COMMON_DIR)/$(NAME:%=%.cpp)
OBJ := $(TEMP_DIR)/$(NAME:%=%.o)

EXES += $(EXE)
OBJS += $(OBJ)

$(OBJ) : $(SRC) $(DEPEND)
	$(CC) -c $(DW_FLAGS) $< -o $@

################################################################################

NAME := map
DEPEND := $(DW_INCLUDE)/$(NAME:%=%.hpp)

DEPEND += $(DW_INCLUDE)/point.hpp

SRC := $(COMMON_DIR)/$(NAME:%=%.cpp)
OBJ := $(TEMP_DIR)/$(NAME:%=%.o)

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

OBJS += $(OBJ)
TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(OBJ) : $(SRC) $(DEPEND)
	$(CC) -c $(DW_FLAGS) $< -o $@

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(TEST_FLAGS) $(DW_FLAGS) $< -o $@

OBJ += $(TEMP_DIR)/noise.o

TEST_OBJ += $(OBJ)
$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(TEST_FLAGS) $(DW_FLAGS) $^ -o $@

################################################################################

NAME := line
DEPEND := 

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(TEST_FLAGS) $(DW_FLAGS) $< -o $@

OBJ := $(TEMP_DIR)/noise.o
OBJ += $(TEMP_DIR)/map.o
OBJ += $(TEMP_DIR)/path.o

TEST_OBJ += $(OBJ)
$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(TEST_FLAGS) $(DW_FLAGS) $^ -o $@

################################################################################

NAME := path
DEPEND := $(DW_INCLUDE)/$(NAME:%=%.hpp)

DEPEND += $(DW_INCLUDE)/point.hpp

SRC := $(COMMON_DIR)/$(NAME:%=%.cpp)
OBJ := $(TEMP_DIR)/$(NAME:%=%.o)

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_%)

OBJS += $(OBJ)
TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(OBJ) : $(SRC) $(DEPEND)
	$(CC) -c $(DW_FLAGS) $< -o $@

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(TEST_FLAGS) $(DW_FLAGS) $< -o $@

OBJ += $(TEMP_DIR)/noise.o
OBJ += $(TEMP_DIR)/map.o

TEST_OBJ += $(OBJ)
$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(TEST_FLAGS) $(DW_FLAGS) $^ -o $@

################################################################################

NAME := json
DEPEND := $(DW_INCLUDE)/world.hpp
DEPEND += $(DW_INCLUDE)/agent_manager.hpp
DEPEND += $(DW_INCLUDE)/scheduler.hpp
DEPEND += $(DW_INCLUDE)/point.hpp
DEPEND += $(DW_INCLUDE)/path.hpp
DEPEND += $(DW_INCLUDE)/map.hpp
DEPEND += $(DW_INCLUDE)/agent.hpp

TEST_SRC := $(TEST_DIR)/$(NAME:%=test_dw_%.cpp)
TEST_OBJ := $(TEMP_DIR)/$(NAME:%=test_dw_%.o)
TEST_EXE := $(EXEC_DIR)/$(NAME:%=test_dw_%)

TEST_EXES += $(TEST_EXE)
TEST_OBJS += $(TEST_OBJ)

$(TEST_OBJ) : $(TEST_SRC) $(DEPEND)
	$(CC) -c $(TEST_FLAGS) $(DW_FLAGS) $< -o $@

OBJ := $(TEMP_DIR)/noise.o
OBJ += $(TEMP_DIR)/map.o
OBJ += $(TEMP_DIR)/path.o

TEST_OBJ += $(OBJ)
$(TEST_EXE) : $(TEST_OBJ) 
	$(CC) $(TEST_FLAGS) $(DW_FLAGS) $^ -o $@

################################################################################

NAME := server
DEPEND := $(NET_INCLUDE)/$(NAME:%=net_%.hpp)

DEPEND += $(NET_INCLUDE)/net_tsdeque.hpp
DEPEND += $(NET_INCLUDE)/net_message.hpp
DEPEND += $(NET_INCLUDE)/net_connection.hpp
DEPEND += $(DW_INCLUDE)/world.hpp
DEPEND += $(DW_INCLUDE)/agent_manager.hpp
DEPEND += $(DW_INCLUDE)/scheduler.hpp
DEPEND += $(DW_INCLUDE)/point.hpp
DEPEND += $(DW_INCLUDE)/path.hpp
DEPEND += $(DW_INCLUDE)/map.hpp
DEPEND += $(DW_INCLUDE)/agent.hpp

SRC := $(SERVER_DIR)/$(NAME:%=%.cpp)
OBJ := $(TEMP_DIR)/$(NAME:%=%.o)
EXE := $(EXEC_DIR)/$(NAME)

OBJS += $(OBJ)
EXES += $(EXE)

$(OBJ) : $(SRC) $(DEPEND)
	$(CC) -c $(NET_FLAGS) $(DW_FLAGS) $< -o $@

OBJ += $(TEMP_DIR)/noise.o
OBJ += $(TEMP_DIR)/map.o
OBJ += $(TEMP_DIR)/path.o

$(EXE) : $(OBJ)
	$(CC) $(NET_FLAGS) $(DW_FLAGS) $^ -o $@

.PHONY	: all
all		: server test

.PHONY	: server
server	: $(EXEC_DIR)/server

.PHONY	: test
test	: $(TEST_EXES)

.PHONY	: clean
clean	:
	rm -f $(CLEAN_LIST)
